package com.hanbang.taitaigou.ui.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;


import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.ui.activity.base.BaseActivity;
import com.hanbang.taitaigou.ui.widget.AnimCheckBox;
import com.hanbang.taitaigou.utils.http.HttpCallBack;
import com.hanbang.taitaigou.utils.http.HttpRequestParams;
import com.hanbang.taitaigou.utils.other.EditHelper;
import com.hanbang.taitaigou.utils.other.StringUtils;
import com.hanbang.taitaigou.utils.other.Validators;
import com.hanbang.taitaigou.utils.ui.ToastUtils;
import com.hanbang.taitaigou.utils.ui.UiUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

public class RegisterActivity extends BaseActivity {

    private EditHelper editHelper = new EditHelper(this);
    // 验证码按钮倒计时 总时长和间隔时长
    private long totalTime = 60000, intervalTime = 1000;

    @ViewInject(R.id.code_button)
    private TextView codeButton;

    @ViewInject(R.id.phone)
    private EditText phone = null;
    @ViewInject(R.id.code)
    private EditText code = null;
    @ViewInject(R.id.checkbox)
    private AnimCheckBox checkbox = null;

    @ViewInject(R.id.password)
    private EditText password = null;
    @ViewInject(R.id.password2)
    private EditText password2 = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        initView();
        setOnClickListener();
    }

    /*
     * 设置点击事件
     */
    private void setOnClickListener() {
    }

    private OnClickListener onClickListener = new OnClickListener() {
        Intent intent = new Intent();

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            }
        }
    };

    /*
     * 初始化view
     */
    private void initView() {
        // 设置顶部的标题
        setBackArrows(findViewById(R.id.top_bar_left));
        setTop(null, R.mipmap.back, "用户注册", null, 0);

        editHelper.setEditText(phone, code, password, password2);
    }

    /*
     */
    private void register() throws IndexOutOfBoundsException {
    }

    /**
     * 发送注册验证码
     */
    @Event(value = R.id.code_button, type = OnClickListener.class)
    private void sendYZMClick(View view) {
        String mobile = phone.getText().toString().trim();

        if (!Validators.isPhoneNumber(mobile)) {
            UiUtils.shakeLeft(phone, 0.9f, 10);
            ToastUtils.showLong(this, "请正确输入手机号");
            return;
        }


        HttpRequestParams params = new HttpRequestParams();
        params.addBodyParameter("action", "UserVerifySmscode");
        params.addBodyParameter("code_type", "1");
        params.addBodyParameter("mobile", mobile);
        httpManager.post(params, new HttpCallBack<String>(this, "正在发送验证码...", view) {

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    // {"status":1, "msg":"短信发送成功！","code":"9160"}
                    JSONObject json = new JSONObject(result);
                    ToastUtils.showShort(RegisterActivity.this, json.getString("msg"));
                    if (json.getInt("status") == 1) {
                        countDownTimer.start();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 立即注册
     */
    @Event(value = R.id.next, type = OnClickListener.class)
    private void nextClick(View view) {
        final String str = phone.getText().toString().trim();
        String yanzm = code.getText().toString().trim();
        final String pass1 = password.getText().toString().trim();
        String pass2 = password2.getText().toString().trim();
        if (!Validators.isPhoneNumber(str)) {
            ToastUtils.showLong(this, "请正确输入手机号");
            UiUtils.shakeLeft(phone, 0.9f, 10);
            return;
        }
        if (StringUtils.isBlank(yanzm)) {
            ToastUtils.showLong(this, "请正确输入验证码");
            UiUtils.shakeLeft(code, 0.9f, 10);
            return;
        }
        if (!Validators.isRegexMatch(pass1, "\\S{6,}")) {
            ToastUtils.showLong(this, "请输入最少6位数密码");
            UiUtils.shakeLeft(password, 0.9f, 10);
            return;
        }
        if (!Validators.isRegexMatch(pass2, "\\S{6,}")) {
            ToastUtils.showLong(this, "请再次输入最少6位数密码");
            UiUtils.shakeLeft(password2, 0.9f, 10);
            return;
        }
        if (!StringUtils.isEquals(pass1, pass2)) {
            ToastUtils.showLong(this, "两次输入的密码不一致");
            UiUtils.shakeLeft(password, 0.9f, 10);
            UiUtils.shakeLeft(password2, 0.9f, 10);
            return;
        }
        if (!checkbox.isClickable()) {
            ToastUtils.showLong(this, "请同意注册协议");
            UiUtils.shakeLeft(checkbox, 0.9f, 10);
            return;
        }
        HttpRequestParams params = new HttpRequestParams();
        params.addBodyParameter("action", "UserRegister");
        params.addBodyParameter("password", pass1);
        params.addBodyParameter("mobile", str);
        params.addBodyParameter("code", yanzm);
        httpManager.post(params, new HttpCallBack<String>(this, "正在注册...", view) {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject json = new JSONObject(result);
                    ToastUtils.showShort(RegisterActivity.this, json.getString("msg"));
                    if (json.getInt("status") == 1) {
                        Intent intent = new Intent();
                        intent.putExtra("phone", str);
                        intent.putExtra("password", pass1);
                        setResult(999, intent);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    @Event(value = R.id.user_register_treaty)
    private void zcxyOnClick(View view) {
        Intent intent = new Intent();
        intent.setClass(RegisterActivity.this,
                RegisterProtocolActivity.class);
        startActivity(intent);
    }

    CountDownTimer countDownTimer = new CountDownTimer(totalTime, intervalTime) {
        @Override
        public void onFinish() {// 计时完毕时触发
            codeButton.setText("重新发送");
            codeButton.setEnabled(true);
            // authCodeButton.setClickable(true);
        }

        @Override
        public void onTick(long currentTime) {// 计时过程显示
            // authCodeButton.setClickable(false);
            codeButton.setEnabled(false);
            codeButton.setText(currentTime / 1000 + "秒");
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
